package com.ridaizu.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.LazyDataModel;

import com.google.common.base.Strings;
import com.ridaizu.cache.CacheDataBean;
import com.ridaizu.dao.db.DBManager;
import com.ridaizu.lazy.LazyGoodsCatalogTypeDataModel;
import com.ridaizu.model.GoodsCatalogType;
import com.ridaizu.model.form.GoodsCatalogTypeForm;
import com.ridaizu.model.search.GoodsCatalogTypeSearch;
import com.ridaizu.service.impl.GoodsCatalogTypeServiceImpl;
import com.ridaizu.util.DateUtil;

@javax.faces.bean.ManagedBean(name = "goodsCatalogTypeBean")
@SessionScoped
public class GoodsCatalogTypeBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private LazyDataModel<GoodsCatalogType> lazyModel;

    @ManagedProperty("#{cacheDataBean}")
    private CacheDataBean cacheDataBean;

    @ManagedProperty("#{goodsCatalogTypeServiceImpl}")
    private GoodsCatalogTypeServiceImpl goodsCatalogTypeService;

    @ManagedProperty("#{goodsCatalogTypeSearch}")
    private GoodsCatalogTypeSearch goodsCatalogTypeSearch;

    @ManagedProperty("#{goodsCatalogTypeForm}")
    private GoodsCatalogTypeForm goodsCatalogTypeForm;

	public void searchGoodsCatalogTypeSync() {
		lazyModel = new LazyGoodsCatalogTypeDataModel(goodsCatalogTypeSearch);
	}

	public void viewRegisterGoodsCatalogType() throws SQLException {

		setInitValues(null);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 200);

        RequestContext.getCurrentInstance().openDialog("goods-catalog-type", options, null);
    }

	public void viewUpdateGoodsCatalogType(GoodsCatalogType goodsCatalogType) throws SQLException {

		setInitValues(goodsCatalogType);

        Map<String,Object> options = new HashMap<String, Object>();
        options.put("modal", true);
        options.put("contentWidth", 550);
        options.put("contentHeight", 220);

        RequestContext.getCurrentInstance().openDialog("goods-catalog-type", options, null);
    }

	private void setInitValues(GoodsCatalogType goodsCatalogType) throws SQLException {
		goodsCatalogTypeForm.clearAll();
		setValues(goodsCatalogType);
	}

	private void setValues(GoodsCatalogType goodsCatalogType) {
		if (goodsCatalogType == null) {
			setRegisterValues();
		} else {
			setUpdateValues(goodsCatalogType);
		}
	}

	private void setRegisterValues() {
	}

	private void setUpdateValues(GoodsCatalogType goodsCatalogType) {
		goodsCatalogTypeForm.setId(String.valueOf(goodsCatalogType.getId()));
		goodsCatalogTypeForm.setGoodsCatalogId(String.valueOf(goodsCatalogType.getGoodsCatalogId()));
		goodsCatalogTypeForm.setGoodsTypeId(String.valueOf(goodsCatalogType.getGoodsTypeId()));
		goodsCatalogTypeForm.setRgtOpt(goodsCatalogType.getRgtOpt());
		goodsCatalogTypeForm.setRgtDate(new Date(goodsCatalogType.getRgtDate().getTime()));
		goodsCatalogTypeForm.setUpdOpt(goodsCatalogType.getUpdOpt());
		goodsCatalogTypeForm.setUpdDate(new Date(goodsCatalogType.getUpdDate().getTime()));
	}

	public void registerGoodsCatalogType() {
		GoodsCatalogType goodsCatalogType = getGoodsCatalogType();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsCatalogTypeService.insertModel(goodsCatalogType);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
    }

	public void updateGoodsCatalogType() {

		GoodsCatalogType goodsCatalogType = getGoodsCatalogType();

		boolean result = false;

		try {
			DBManager.beginTransaction();
			result = goodsCatalogTypeService.updateModel(goodsCatalogType);
			DBManager.commitTransaction();

		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		RequestContext.getCurrentInstance().closeDialog(result);
	}

	public void deleteGoodsCatalogType() {

		GoodsCatalogType selectedGoodsCatalogType = goodsCatalogTypeForm.getSelectedGoodsCatalogType() ;

		if (selectedGoodsCatalogType == null || selectedGoodsCatalogType.getId() < 1) {
	        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", "请选择要删除的商品目录信息。");
	        FacesContext.getCurrentInstance().addMessage(null, message);

	        return ;
		}

		boolean result = false;
		try {
			DBManager.beginTransaction();
			result = goodsCatalogTypeService.deleteModel(selectedGoodsCatalogType.getId());
			DBManager.commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			result = false;
			DBManager.rollbackTransaction();
		}

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品目录[%s]删除成功。", selectedGoodsCatalogType.getId());
		} else {
			resultStr = String.format("商品目录[%s]删除失败。", selectedGoodsCatalogType.getId());
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "GoodsCatalogType delete result", resultStr);
        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void registerGoodsCatalogTypeResult(SelectEvent event) {
		boolean result = (Boolean) event.getObject();

		handleResult(result, "登录");
	}

	public void updateGoodsCatalogTypeResult(SelectEvent event) {

		boolean result = (Boolean) event.getObject();

		handleResult(result, "更新");
	}

	private void handleResult(boolean result, String optStr) {

		String resultStr = null;
		if (result) {
			resultStr = String.format("商品目录信息%s成功。", optStr);
		} else {
			resultStr = String.format("商品目录信息%s失败。", optStr);;
		}

        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Message", resultStr);

        FacesContext.getCurrentInstance().addMessage(null, message);
	}

	private GoodsCatalogType getGoodsCatalogType() {

		GoodsCatalogType goodsCatalogType = new GoodsCatalogType();

		if (!Strings.isNullOrEmpty(goodsCatalogTypeForm.getId())) {
			goodsCatalogType.setId(Integer.valueOf(goodsCatalogTypeForm.getId()));
		}

		if (!Strings.isNullOrEmpty(goodsCatalogTypeForm.getGoodsCatalogId())) {
			goodsCatalogType.setGoodsCatalogId(Integer.valueOf(goodsCatalogTypeForm.getGoodsCatalogId()));
		}

		if (!Strings.isNullOrEmpty(goodsCatalogTypeForm.getGoodsTypeId())) {
			goodsCatalogType.setGoodsTypeId(Integer.valueOf(goodsCatalogTypeForm.getGoodsTypeId()));
		}

		Timestamp currentDate = DateUtil.getCurrentDateTime();

		goodsCatalogType.setRgtOpt("Admin");
		goodsCatalogType.setRgtDate(currentDate);
		goodsCatalogType.setUpdOpt("Admin");
		goodsCatalogType.setUpdDate(currentDate);

		return goodsCatalogType;
	}

	public LazyDataModel<GoodsCatalogType> getLazyModel() {
		return lazyModel;
	}

	public void setLazyModel(LazyDataModel<GoodsCatalogType> lazyModel) {
		this.lazyModel = lazyModel;
	}

	public GoodsCatalogTypeServiceImpl getGoodsCatalogTypeService() {
		return goodsCatalogTypeService;
	}

	public void setGoodsCatalogTypeService(GoodsCatalogTypeServiceImpl goodsCatalogTypeService) {
		this.goodsCatalogTypeService = goodsCatalogTypeService;
	}

	public GoodsCatalogTypeSearch getGoodsCatalogTypeSearch() {
		return goodsCatalogTypeSearch;
	}

	public void setGoodsCatalogTypeSearch(GoodsCatalogTypeSearch goodsCatalogTypeSearch) {
		this.goodsCatalogTypeSearch = goodsCatalogTypeSearch;
	}

	public GoodsCatalogTypeForm getGoodsCatalogTypeForm() {
		return goodsCatalogTypeForm;
	}

	public void setGoodsCatalogTypeForm(GoodsCatalogTypeForm goodsCatalogTypeForm) {
		this.goodsCatalogTypeForm = goodsCatalogTypeForm;
	}

	public CacheDataBean getCacheDataBean() {
		return cacheDataBean;
	}

	public void setCacheDataBean(CacheDataBean cacheDataBean) {
		this.cacheDataBean = cacheDataBean;
	}

}